﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxEditors;

namespace UdrinkWater_Project.Customer
{
    public partial class Order : System.Web.UI.Page
    { 
        
        string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void DataViewGoods_ItemCommand(object source, DevExpress.Web.ASPxDataView.DataViewItemCommandEventArgs e)
        {
            //if (e.CommandName == "Cmd")
            //{
            //    for (int i = 0; i < DataViewGoods.VisibleItems.Count; i++)
            //    {
            //        ASPxTextBox ProductID = (ASPxTextBox)DataViewGoods.FindItemControl("lblName", DataViewGoods.Items[i]);
            //        ASPxTextBox TextPrice = (ASPxTextBox)DataViewGoods.FindItemControl("Tb_Price", DataViewGoods.Items[i]);
            //        ASPxLabel TextUnit = (ASPxLabel)DataViewGoods.FindItemControl("lb_Unit", DataViewGoods.Items[i]);

            //        if (TextPrice.Text != "")
            //        {
            //            string Goods_ID = ProductID.Text;
            //            string Number = TextPrice.Text;
            //            string Unit = TextUnit.Text;
            //            TextPrice.Text = "";

            //            AddGoods(Goods_ID, Number, Unit);
            //        }
            //        else
            //        {
                        
            //        }
            //    }
                
                

            //}
        }

        public void AddGoods(string GoodID, string Number, string Unit)
        {
            string CheckUnit = "";
            if (Unit == "แพ็ค")
            {
                if (Convert.ToInt32(Number) >= 50)
                {
                    CheckUnit = "มีส่วนลด";
                }
                else
                {
                    CheckUnit = "ไม่มีส่วนลด";
                }
            }
            DataTable dt = new DataTable();
            dt = GetData(GoodID);

            if (Session["TablePrice"] == null)
            {
                DataTable ListPrice = new DataTable();
                DataRow Row_Price = null;

                //  ListPrice.Columns.Add(new DataColumn("Customer_ID", typeof(string)));
                ListPrice.Columns.Add(new DataColumn("Goods_ID", typeof(string)));
                ListPrice.Columns.Add(new DataColumn("Goos_Name", typeof(string)));
                ListPrice.Columns.Add(new DataColumn("Price", typeof(int)));
                ListPrice.Columns.Add(new DataColumn("Unit", typeof(string)));
                ListPrice.Columns.Add(new DataColumn("PriceNumber", typeof(int)));
                ListPrice.Columns.Add(new DataColumn("Discount", typeof(string)));


                Row_Price = ListPrice.NewRow();
                Row_Price["Goods_ID"] = GoodID;
                Row_Price["Goos_Name"] = dt.Rows[0]["Product_name"].ToString();
                Row_Price["Price"] = Convert.ToInt32(dt.Rows[0]["Price"]);
                Row_Price["Unit"] = dt.Rows[0]["Unit_Name"].ToString();
                Row_Price["PriceNumber"] = Convert.ToInt32(Number);
                Row_Price["Discount"] = CheckUnit;
                ListPrice.Rows.Add(Row_Price);

                Session["TablePrice"] = ListPrice;
            }
            else
            {
                DataTable ListPrice = (DataTable)Session["TablePrice"];

                DataRow Row_Price = ListPrice.NewRow();
                Row_Price["Goods_ID"] = GoodID;
                Row_Price["Goos_Name"] = dt.Rows[0]["Product_name"].ToString();
                Row_Price["Price"] = Convert.ToInt32(dt.Rows[0]["Price"]);
                Row_Price["Unit"] = dt.Rows[0]["Unit_Name"].ToString();
                Row_Price["PriceNumber"] = Convert.ToInt32(Number);
                Row_Price["Discount"] = CheckUnit;
                ListPrice.Rows.Add(Row_Price);

                Session["TablePrice"] = ListPrice;
            }

        }
        private DataTable GetData(string GoodsID)
        {
            string sqltext = @"SELECT        TB_Product.Product_ID, TB_Product.Product_name, TB_Unit_Category.Unit_Name, TB_Product.Price, TB_Product.In_Stock, TB_Product.Image_Path
FROM            TB_Product LEFT JOIN
                         TB_Unit_Category ON TB_Product.Unit = TB_Unit_Category.Unit_ID WHERE TB_Product.Product_ID = '" + GoodsID + "' ";
            string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);

            return ds.Tables[0];

        }

        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < DataViewGoods.VisibleItems.Count; i++)
            {
                ASPxLabel ProductID = (ASPxLabel)DataViewGoods.FindItemControl("lblName", DataViewGoods.Items[i]);
                ASPxTextBox TextPrice = (ASPxTextBox)DataViewGoods.FindItemControl("Tb_Price", DataViewGoods.Items[i]);
                ASPxLabel TextUnit = (ASPxLabel)DataViewGoods.FindItemControl("lb_Unit", DataViewGoods.Items[i]);

                if (TextPrice.Text != "")
                {
                    string Goods_ID = ProductID.Text;
                    string Number = TextPrice.Text;
                    string Unit = TextUnit.Text;
                    TextPrice.Text = "";

                    AddGoods(Goods_ID, Number, Unit);
                }
                else
                {

                }
               
            } 
            Response.Redirect("ShowOrderList.aspx");
        }

        protected void btCancleOrder_Click(object sender, EventArgs e)
        {
           Response.Redirect("Order.aspx");
        }
    }
}